/**
 *
 * jcrt and its source-code is licensed under the LGPL.
 * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
 * When copying, adapting, or redistributing this document in keeping with the guidelines above,
 * you are required to provide proper attribution to obinary.
 * If you reproduce or distribute the document without making any substantive modifications to its content,
 * please use the following attribution line:
 *
 * Copyright 2007 Atjava.com Ltd. (http://www.atjava.com) All rights reserved.
 *
 */
package com.atjava.jcrt.utility;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;

import com.atjava.jcrt.JcrtSessionFactory;
import com.atjava.jcrt.core.SystemProperty;
import com.atjava.jcrt.core.exception.CoreException;

/**
 * 
 * @author biggie
 * @version $Revision: 1.3 $ $Date: 2008/08/07 00:52:23 $
 */
public class StartRepositoryListener implements ServletContextListener {

    /**
     * Log4j
     */
    private Logger log = Logger.getLogger(this.getClass().getName());
    
	public void contextDestroyed(ServletContextEvent arg0) {

	}

	public void contextInitialized(ServletContextEvent arg0) {
		try{
			PropertyHelper helper = new PropertyHelper();

			SystemProperty.setProperty("ADMIN_USERNAME", helper.getValue("ADMIN_USERNAME"));
			SystemProperty.setProperty("ADMIN_PASSWORD", helper.getValue("ADMIN_PASSWORD"));
			
			String path = arg0.getServletContext().getRealPath("");
			if(path!=null && path.indexOf("/./")!=1){
				path.replaceAll("/./", "/");
			}
			
			SystemProperty.SYSTEM_REALPATH = path;
			
			try {
				JcrtSessionFactory factory = JcrtSessionFactory.getInstance();
				if(!factory.getStartServiceFlag()){
					factory.startServices();
				}
			} catch (CoreException e) {
				log.error("Start Repository Services faild !",e);
			}
		}catch(Exception e){}

	}

}
